package com.QK.cnstudy;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.RadioButton;

/**
 * 
 * @author newfarking
 * 
 *         2012-10-29添加 自动切换
 * 
 */
public class MyViewPagerWithBottom extends ViewPager implements OnTouchListener, View.OnClickListener {

    /**
     * 是否继续自动切换
     * 
     * 在手指按下时取消自动切换
     * 
     * 松开继续启动自动切换
     */
    public boolean isContinue = true;
    /**
     * 底部圆点布局
     */
    public LinearLayout pointLayout;

    public List<RadioButton> radioButtons;

    /**
     * page个数 0...count-1
     * 
     * 1...count-2为有效
     * 
     */
    public int count = 0;
    public ArrayList<View> pageViews;

    public MyViewPagerWithBottom(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public MyViewPagerWithBottom(Context context) {
        super(context);
    }

    public void setPointLayout(LinearLayout pointLayout) {
        this.pointLayout = pointLayout;
    }

    public void setRadioButtons(List<RadioButton> radioButtons) {
        this.radioButtons = radioButtons;
    }

    public void setViewList(ArrayList<View> pageViews) {
        this.pageViews = pageViews;
        setAdapter(new GuidePageAdapter());
        setOnPageChangeListener(new GuidePageChangeListener());
        setCurrentItem(1);
        count = pageViews.size();
        initPoint();
        setOnTouchListener(this);
        // 定时滑动线程
        /*
         * new Thread(new Runnable() {
         * 
         * @Override public void run() { while (true) { try {
         * Thread.sleep(5000); } catch (InterruptedException e) { // TODO
         * Auto-generated catch block e.printStackTrace(); } if (isContinue) {
         * int item = getCurrentItem(); item++; if (item == count - 1) { item =
         * 1; }
         * 
         * viewHandler.sendEmptyMessage(item); } } } }).start();
         */
    }

    public void initPoint() {
        for (int i = 0; i < radioButtons.size(); i++) {
            radioButtons.get(i).setOnClickListener(this);
        }
        radioButtons.get(0).setChecked(true);
    }

    public void changePoint(int index) {
        for (int i = 0; i < radioButtons.size(); i++) {
            if (i == index - 1) {
                radioButtons.get(i).setChecked(true);
            } else {
                radioButtons.get(i).setChecked(false);
            }
        }
    }

    /** 指引页面Adapter */
    class GuidePageAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return pageViews.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == arg1;
        }

        @Override
        public int getItemPosition(Object object) {
            // TODO Auto-generated method stub
            return super.getItemPosition(object);
        }

        @Override
        public void destroyItem(View arg0, int arg1, Object arg2) {
            // TODO Auto-generated method stub
            ((ViewPager) arg0).removeView(pageViews.get(arg1));
        }

        @Override
        public Object instantiateItem(View arg0, int arg1) {
            // TODO Auto-generated method stub
            ((ViewPager) arg0).addView(pageViews.get(arg1));
            return pageViews.get(arg1);
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public Parcelable saveState() {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public void startUpdate(View arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void finishUpdate(View arg0) {
            // TODO Auto-generated method stub

        }
    }

    /** 指引页面改监听器 */
    class GuidePageChangeListener implements OnPageChangeListener {

        @Override
        public void onPageScrollStateChanged(int arg0) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onPageSelected(int arg0) {

            if (arg0 == 0) {
                setCurrentItem(arg0 + 1);
                return;
            }

            if (arg0 == count - 1) {
                setCurrentItem(arg0 - 1);
                return;
            }
            changePoint(arg0);
        }
    }

    /**
     * 监控手指按下事件
     * 
     * 来判断是否自动切换
     */
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
        case MotionEvent.ACTION_MOVE:
            isContinue = false;
            break;
        case MotionEvent.ACTION_UP:
            isContinue = true;
            break;
        default:
            isContinue = true;
            break;
        }
        return false;
    };

    /**
     * 处理定时切换广告栏图片的句柄
     */
    private final Handler viewHandler = new Handler() {
        @Override
        public void handleMessage(final Message msg) {

            setCurrentItem(msg.what);
            super.handleMessage(msg);
        }
    };

    @Override
    public void onClick(View v) {
        for (int i = 0; i < radioButtons.size(); i++) {
            if (radioButtons.get(i).getId() == v.getId()) {
                setCurrentItem(i + 1);
            }
        }
    }
}
